<template>
  <div class="app-container">
    <el-row class="head">
      <span>编号:{{course.courseId}}</span>
      <span>学分:{{course.credit}}</span>
      <span>课时:{{course.periods}}</span>
      <span>名称:{{course.courseName}}</span>
      <span>学院:{{course.courseCollege}}</span>
      <span>班级:{{course.courseClass}}</span>
      <span>学期:{{course.courseSemester}}</span>
      <span>备注:{{course.notes}}</span>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="3">
        <el-form >
          <el-form-item >
              <el-image class="img" :src="course.headImg"  fit="full">
                <div slot="error" class="image-slot">
                  <el-image :src="defaultImg"  fit="full"></el-image>
                </div>
              </el-image>
          </el-form-item>
          <el-form-item>
            <el-button class="btn" type="primary" @click="Gohome">返回</el-button>
          </el-form-item>
          <el-form-item>
            <el-button  class="btn" type="primary" plain @click="upCourse">修改课程</el-button>
          </el-form-item>
          <el-form-item>
            <el-button class="btn"  id="studentBtn" plain  @click="studentList">学生管理</el-button>
          </el-form-item>
          <el-form-item>
            <el-button class="btn" id="topicBtn" plain  @click="topicList">题目管理</el-button>
          </el-form-item>
          <el-form-item>
            <el-button class="btn" id="annexBtn" plain  @click="annexList">附件管理</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :span="21">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
          <el-form-item :label="manTitle+'管理'" prop="roleName">
            <el-input
              v-model="queryParams.annexName"
              :placeholder="'请输入'+manTitle+'编号'"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
          </el-form-item>
        </el-form>

        <el-table v-loading="loading" :data="roleList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column :label="manTitle+'编号'" prop="annexId" width="170" />
          <el-table-column :label="manTitle+'名称'" prop="annexName" :show-overflow-tooltip="true" width="170" />
          <el-table-column label="备注" prop="annexNotes" width="400" />
          <el-table-column label="创建时间" align="center" prop="createTime" width="120">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope" v-if="scope.row.roleId !== 1">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <!--分页组件-->
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 修改配置对话框 -->
        <el-dialog :title="'修改'+manTitle" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item :label="manTitle+'名称'" prop="name">
              <el-input v-model="form.Name" :placeholder="'请输入'+manTitle+'名称'" />
            </el-form-item>
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-col>
    </el-row>
    <addStudent :addStuBtn="addStuBtn" @offAddStu="offAddStu"></addStudent>
    <addTopic :addTopBtn="addTopBtn" @offAddTop="offAddTop"></addTopic>
    <addAnnex :addAnnBtn="addAnnBtn" @offAddAnn="offAddAnn"></addAnnex>
    <upCourse :upCourseBtn="upCourseBtn" :course="course" @offUpCourse="offUpCourse"></upCourse>
  </div>
</template>

<script>
import { listRole, getRole, delRole, addRole, updateRole, dataScope, changeRoleStatus, deptTreeSelect } from "@/api/system/role";
import addTopic from "@/views/system/teaching/addTeaching/addTopic.vue";
import addStudent from "@/views/system/teaching/addTeaching/addStudent.vue";
import addAnnex from "@/views/system/teaching/addTeaching/addAnnex.vue";
import upCourse from "@/views/teacher/course/upCourse.vue";
import {getCourse} from "@/api/system/course";
import course_cover from '../../../assets/images/course-cover.png'

export default {
  components: { addAnnex, addStudent, addTopic ,upCourse},
  data() {
    return {
      //默认图片
      defaultImg:course_cover,
      addStuBtn:false,
      addTopBtn:false,
      addAnnBtn:false,
      upCourseBtn:false,
      //页面字段
      manTitle:"",
      //课程信息
      course:{
        courseId:null,
        courseName:"",
        courseCollege:"计算机学院",
        courseSemester:"2024年春",
        courseClass:"计算机科学与技术",
        credit:null,//学分
        periods:null,//课时
        notes:null,//备注
        headImg:null,//封面
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 角色表格数据
      roleList: [],
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: `${this.manTitle}名称不能为空`, trigger: "blur" }
        ],
      }
    };
  },
  //创建实例运行
  created() {
    this.course.courseId=this.$route.query.courseId;
    this.getList();
  },
  //加载时运行
  mounted() {
    this.getCourseData(this.course.courseId);
    this.studentList();
  },
  methods: {
    /**获取课程信息*/
    getCourseData(id){
      getCourse(id).then(res=>{
        this.course={
          courseId:res.data.courseId,
          courseName:res.data.courseName,
          courseCollege:"计算机学院",
          courseSemester:"2024年春",
          courseClass:"计算机科学与技术",
          credit:res.data.credit,
          periods:res.data.periods,
          notes:res.data.content,
          headImg:process.env.VUE_APP_BASE_API+res.data.headImg,
        }
      })
    },
    /**修改课程*/
    upCourse(){
      this.upCourseBtn=true;
    },
    /**返回*/
    Gohome(){
      this.$router.push({ path: "/system/course" });
    },
    /**多选框选中数据*/
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roleId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /**去除按钮激活状态*/
    removeActive(n){
      document.getElementById('studentBtn').classList.remove('btn_active')
      document.getElementById('topicBtn').classList.remove('btn_active')
      document.getElementById('annexBtn').classList.remove('btn_active')
      switch (n){
        case 1:
          document.getElementById('studentBtn').classList.add('btn_active');
          break;
        case 2:
          document.getElementById('topicBtn').classList.add('btn_active');
          break;
        case 3:
          document.getElementById('annexBtn').classList.add('btn_active');
      }
    },
    /**加载学生列表*/
    studentList(){
      this.removeActive(1);
      this.manTitle="学生";
      console.log("学生列表")
    },
    /**加载题目列表*/
    topicList(){
      this.removeActive(2);
      this.manTitle="题目";
      console.log("题目列表")
    },
    /**加载附件列表*/
    annexList(){
      this.removeActive(3);
      this.manTitle="附件";
      console.log("附件列表")
    },
    /** 查询数据列表 */
    getList() {
      this.loading = false;
      this.roleList.push({
        annexId: "1",
        annexName: "STM32",
        annexNotes: "无",
        createTime: "",
      })
      // this.loading = true;
      // listRole(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
      //     this.roleList = response.rows;
      //     this.total = response.total;
      //     this.loading = false;
      //   }
      // );
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      if (this.$refs.menu != undefined) {
        this.$refs.menu.setCheckedKeys([]);
      }
      this.menuExpand = false,
        this.menuNodeAll = false,
        this.deptExpand = true,
        this.deptNodeAll = false,
        this.form = {
          Id: undefined,
          Name: undefined,
          status: "0",
          menuIds: [],
          deptIds: [],
          menuCheckStrictly: true,
          deptCheckStrictly: true,
          remark: undefined
        };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      if(this.manTitle==="学生"){
        this.addStuBtn=true;
      }else if(this.manTitle==="附件"){
        this.addAnnBtn=true;
      }else if(this.manTitle==="题目"){
        this.addTopBtn=true;
      }
    },
    /**关闭添加学生对话框*/
    offAddStu(newValue){
      this.addStuBtn=newValue;
    },
    /**关闭添加题目对话框*/
    offAddTop(newValue){
      this.addTopBtn=newValue;
    },
    /**关闭添加附件对话框*/
    offAddAnn(newValue){
      this.addAnnBtn=newValue;
    },
    /**关闭修改课程对话框*/
    offUpCourse(newValue){
      this.upCourseBtn=newValue.btn;
      if(newValue.isdel){
        this.Gohome();
      }else{
        this.getCourseData(this.course.courseId);
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      const annexId = row.annexId || this.ids
      // getRole(roleId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.$nextTick(() => {
      //     roleMenu.then(res => {
      //       let checkedKeys = res.checkedKeys
      //       checkedKeys.forEach((v) => {
      //         this.$nextTick(()=>{
      //           this.$refs.menu.setChecked(v, true ,false);
      //         })
      //       })
      //     });
      //   });
      // });
    },

    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.roleId != undefined) {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            updateRole(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.menuIds = this.getMenuAllCheckedKeys();
            addRole(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮（数据权限） */
    submitDataScope: function() {
      if (this.form.roleId != undefined) {
        this.form.deptIds = this.getDeptAllCheckedKeys();
        dataScope(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.openDataScope = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const roleIds = row.roleId || this.ids;
      this.$modal.confirm('是否确认删除角色编号为"' + roleIds + '"的数据项？').then(function() {
        return delRole(roleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/role/export', {
        ...this.queryParams
      }, `role_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped>
.head{
  display: flex; /* 使用Flexbox */
  margin-bottom: 2%;
  border-bottom: 1px solid #ccc;
  gap: 2%; /* 设置子元素之间的间隔为2% */
  align-items: flex-start; /* 让子元素垂直对齐到起点（flex-start） */
}
.btn_active{
  border-color: rgb(68,142,247); /* 边框颜色变为蓝色 */
  color: rgb(68,142,247); /* 字体颜色变为蓝色 */
}
.btn{
  width: 100%;
}
.img{
  width: 100%;
  border-radius: 3%;
  border: 1px solid rgb(198, 198, 201);
}
</style>
